package com.fancy.learncenter.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CourseDataBean;
import com.fancy.learncenter.bean.MySchoolDetailDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.FragPagerAdapter;
import com.fancy.learncenter.ui.fragment.CourseDetailFragment;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class CourseDetailActivity extends BaseActivity {
    public static final int REQUEST_CODE = 100;

    ArrayList<View> vpView = new ArrayList<>();
    @Bind(R.id.view_pager)
    ViewPager viewPager;
    @Bind(R.id.tab_layout)
    TabLayout tabLayout;

    List<Fragment> fragments;
    List titles;
    int type;
    @Bind(R.id.right)
    TextView right;
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_detail, Status.STATUS);
        ButterKnife.bind(this);
        type = getIntent().getIntExtra("type", 0);
        titles = new ArrayList();
        fragments = new ArrayList<>();
        setSupportActionBar(toolbar);

        Constant.isBind = false;
//        setRight("选择学校", new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });

        getSchoolDeatail();
        getData();
    }

    private void getData() {
        HttpMehtod.getInstance().getCourseData(type, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                //解析数据
                BaseBean<CourseDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CourseDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CourseDataBean>() {
                    @Override
                    public void success(CourseDataBean dataBean) {
                        for (int i = 0; i < dataBean.getLessonCategoryVOList().size(); i++) {
                            CourseDetailFragment courseDetailFragment = new CourseDetailFragment();
                            courseDetailFragment.setData(dataBean.getLessonCategoryVOList().get(i));
                            fragments.add(courseDetailFragment);
                            titles.add(dataBean.getLessonCategoryVOList().get(i).getName());
                        }

                        FragPagerAdapter mFragmentAdapteradapter = new FragPagerAdapter(getSupportFragmentManager(), fragments, titles);
                        //给ViewPager设置适配器
                        viewPager.setAdapter(mFragmentAdapteradapter);
                        tabLayout.setTabMode(TabLayout.MODE_FIXED);
                        tabLayout.setupWithViewPager(viewPager);
                    }

                    @Override
                    public void fail(String errorCode) {

                    }

                }).dealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();
            }
        });
    }

//    @OnClick(R.id.school_ll)
//    public void onClick() {
//        Intent intent = new Intent(this, SelectCityActivity.class);
//        startActivity(intent);
//    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


    }


    String schoolName;

    public static String schoolID;

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (!TextUtils.isEmpty(Constant.schoolID)) {

            schoolID = Constant.schoolID;

            LogUtil.MyLog("schoolId", "======schoolId====" + schoolID);
        }
        if (!TextUtils.isEmpty(Constant.schoolName)) {
            schoolName = Constant.schoolName;

            right.setText(schoolName);
        }
    }

//    @Override
//    protected void onResume() {
//        super.onResume();
//
//        if (!TextUtils.isEmpty(Constant.schoolID)) {
//
//            schoolID = Constant.schoolID;
//
//            LogUtil.MyLog("schoolId", "======schoolId====" + schoolID);
//        }
//        if (!TextUtils.isEmpty(Constant.schoolName)) {
//            schoolName = Constant.schoolName;
//
//            right.setText(schoolName);
//        }
//
//    }


    private void getSchoolDeatail() {
        HttpMehtod.getInstance().getMySchoolDeatail(new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<MySchoolDetailDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), MySchoolDetailDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<MySchoolDetailDataBean>() {
                    @Override
                    public void success(MySchoolDetailDataBean dataBean) {
                        //  绑定过学校
                        if (dataBean.getResult().getSelectResult() == 1) {
                            schoolName = dataBean.getResult().getSchoolName();
                            schoolID = dataBean.getResult().getSchoolId() + "";
                            right.setText(dataBean.getResult().getSchoolName());
                        }
                    }

                    @Override
                    public void fail(String errorCode) {

                    }

                }).customDealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();
            }
        });
    }

    @Override
    public void finish() {
        super.finish();

        Constant.schoolName = "";
        Constant.schoolID = "";
    }

    @OnClick({R.id.back, R.id.school_ll})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.school_ll:
                Intent intent = new Intent(this, SelectCityActivity.class);
                startActivity(intent);
                break;
        }
    }
}
